package com.xcm.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.xcm.constant.DateFormatConstant;
import com.xcm.constant.OperationLogConstant;
import com.xcm.constant.PageConstant;
import com.xcm.dao.OperationLogMapper;
import com.xcm.model.TbDeviceLog;
import com.xcm.model.TbOperationLog;
import com.xcm.model.dto.OperationLogListDTO;
import com.xcm.model.vo.OperationLogListVO;
import com.xcm.page.PageInfo;
import com.xcm.service.OperationLogService;
import com.xcm.util.DateUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.util.List;

/**
 * 日志Service实现类
 */
@Service(value = "operationLogService")
public class OperationLogServiceImpl implements OperationLogService {
	@Autowired
	private OperationLogMapper operationLogMapper;

	/**
	 * 保存日志
	 * @param operationLog  TbOperationLog
	 */
	@Override
	@Transactional(readOnly = false, rollbackFor = Exception.class)
	public void saveOperationLog(TbOperationLog operationLog) {
		operationLogMapper.saveOperationLog(operationLog);
	}

	/**
	 * 获取操作日志分页列表
	 *
	 * @param startTime 开始时间
	 * @param endTime   接受时间
	 * @param pageNum   当前页
	 * @param pageSize  每页显示条数
	 * @return PageInfo<OperationLogListVO>
	 */
	@Override
	public PageInfo<OperationLogListVO> listOperationLogByPage(String startTime, String endTime, Integer pageNum, Integer pageSize) {
		//设置默认值
		if (pageNum == null) {
			pageNum = PageConstant.DEFAULT_PAGE_NUM;
		}
		if (pageSize == null) {
			pageSize = PageConstant.DEFAULT_PAGE_SIZE;
		}
		//把格式化字符串时间转化为Long类型时间戳
		Long longStartTime = null;
		Long longEndTime = null;
		try {
			if (StringUtils.isNotBlank(startTime)) {
				longStartTime = DateUtils.stringTimeToLongTimeStamp(startTime, DateFormatConstant.LONG_DATE_FORMAT_STR);
			}
			if (StringUtils.isNotBlank(endTime)) {
				longEndTime = DateUtils.stringTimeToLongTimeStamp(endTime, DateFormatConstant.LONG_DATE_FORMAT_STR);
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		PageHelper.startPage(pageNum, pageSize);
		Page<OperationLogListDTO> operationLogPage = operationLogMapper.listOperationLogByPage(longStartTime, longEndTime);
		//把Page转化为PageInfo
		PageInfo<OperationLogListVO> pageInfo = new PageInfo<>();
		pageInfo.setPageNum(pageNum);
		pageInfo.setPageSize(pageSize);
		pageInfo.setTotal(operationLogPage.getTotal());
		//把时间戳格式化,将成功或失败的数据表现形式转化为字符串
		List<OperationLogListDTO> operationLogDtoList = operationLogPage.getResult();
		List<OperationLogListVO> operationLogVoList = Lists.newArrayList();
		if (CollectionUtils.isNotEmpty(operationLogDtoList)) {
			for (OperationLogListDTO operationLogDto : operationLogDtoList) {
				OperationLogListVO operationLogVO = new OperationLogListVO();
				operationLogVO.setOperationLogId(operationLogDto.getOperationLogId());
				operationLogVO.setOperationTypeName(operationLogDto.getOperationTypeName());
				operationLogVO.setOperationContent(operationLogDto.getOperationContent());
				operationLogVO.setUserName(operationLogDto.getUserName());
				operationLogVO.setIsSuccess(operationLogDto.getIsSuccess());
				Long operationTime = operationLogDto.getOperationTime();
				if (operationTime == null) {
					operationLogVO.setOperationTime("");
				} else {
					String stringDate = DateUtils.longTimeStampToStringDate(operationLogDto.getOperationTime(), DateFormatConstant.LONG_DATE_FORMAT_STR);
					operationLogVO.setOperationTime(stringDate);
				}
				operationLogVoList.add(operationLogVO);
			}
		}
		pageInfo.setList(operationLogVoList);
		return pageInfo;
	}
}
